package org.neowei.image.filtering.handle;

import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.swing.ImageIcon;

import org.neowei.image.api.ImageOption;
import org.neowei.image.filter.Filter;

public class WaterHandle implements IHandle {
	
	@Override
	public BufferedImage getBufferedImage(ImageOption imageOption, BufferedImage srcimage) {
		BufferedImage buffImg = new ResizeHandle().getBufferedImage(imageOption, srcimage);
		Graphics2D g = buffImg.createGraphics();
		//g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
		//g.drawImage(buffImg.getScaledInstance(imageOption.getSrcwidth(), imageOption.getSrcheight(), Image.SCALE_SMOOTH), 0, 0, null);
		ImageIcon imgIcon = new ImageIcon(imageOption.getWaterpath() + File.separatorChar + imageOption.getWaterfilename());
		int[] waterXY = Filter.getWaterXY(imageOption, imgIcon.getIconWidth(), imgIcon.getIconHeight());
		if (waterXY[0] < 0 || waterXY[1] < 0) {
			return srcimage;
		} else {
			Image img = imgIcon.getImage();
			g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1.0f));
			g.drawImage(img, waterXY[0], waterXY[1], null);
			g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
			g.dispose();
			return buffImg;
		}
	}
}
